var url = "http://localhost/project/storeShop"; 

if_res_sub();
$("#swi_dl_zc").click(function(){
    // if_res_sub();
    if(location.hash=="#type=register"){
        location.hash="";
    }else{
        location.hash="type=register";
    }
    if_res_sub();
})
function if_res_sub(){
    if(location.hash==""){
        console.log(1)
        $("#code_div").css("display","none");
        $("#btnRes").css("display","none");
        $("#btnSub").css("display","inline-block");
        $("#swi_dl_zc").text("没有账号去注册");
        
    }else{
        $("#code_div").css("display","flex");
        $("#btnSub").css("display","none");
        $("#btnRes").css("display","inline-block");
        $("#swi_dl_zc").text("已有账号去登陆");
    }
    $("#name").val("");
    $("#password").val("");
    $("#code").val("");
}


$("#btnRes,#btnSub").on("click", function (e) {
    var name = $("#name").val();
    var password = $("#password").val();
    var code = $("#code").val();
    var pwdTest = /^[a-zA-Z]\w{3,17}$/; //验证密码正则(以字母开头，长度在4~18之间，只能包含字母、数字和下划线)
    var nameTest = /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/; //验证用户名正则((字母开头，允许3-16字节，允许字母数字下划线)
    console.log(e)
    console.log(this.value)
    if (nameTest.test(name) && pwdTest.test(password)) {
        if (this.value == "res") {
            $.ajax({
                type: "post",
                url: url + "/php/register.php", 
                data: {
                    "userName": name,
                    "ps": password,
                    "code": code,
                },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    hint(data.msg);
                    if(data.state){
                        setTimeout(function(){
                            location.hash="";
                            if_res_sub();
                        },1300)
                    }
                }
            });
        } else if (this.value == "sub") {
            $.ajax({
                type: "post",
                url: url + "/php/login.php",
                data: {
                    "userName": name,
                    "ps": password
                },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    if (data.state) {
                        //localStorage.setItem("token",data.token);
                        window.location.href ="userList.html";
                    }else{
                       
                        hint(data.msg);
                    }
                }
            });
        }
    } else {
        console.log(name)
        hint("请输入正确的用户名或密码");
    }
});


function hint(data){
    var div;
    div=$("#hint");
    if(div.length<=0){
        div=document.createElement("div");
        $("body").prepend(div);
        console.log(div);                
        div.id="hint";
        div.clientWidth; //查询，防止浏览器同时插入没有动画效果
        console.log("------------");
        div=$("#hint");
    }
    div.text(data);
    div.css("transform","translate(-50%) scale(1)");
    var timer=setTimeout(function(){
        clearTimeout(timer);
        div.css("transform","translate(-50%) scale(0)");
    },2000)
    console.log("=============");
    return true;
}



